<?php

namespace app\admin\controller\auth;

use app\admin\controller\AdminController;
use app\admin\validate\auth\AdminValidate;
use app\admin\service\auth\AdminService;
use app\admin\validate\auth\EditSelfValidate;
use think\response\Json;

/**
 * 管理员控制器
 */
class Admin extends AdminController
{

    /**
     * 查看管理员列表
     * @return Json
     */
    public function lists()
    {
        return $this->data(AdminService::getList());
    }


    /**
     * 添加管理员
     * @return Json
     */
    public function add()
    {
        $params = (new AdminValidate())->post()->checkParam('add');
        $result = AdminService::add($params);
        if (true === $result) {
            return $this->success([],'操作成功');
        } else {
            return $this->error(AdminService::getError());
        }
    }


    /**
     * 编辑管理员
     * @return Json
     */
    public function edit()
    {
        $params = (new AdminValidate())->post()->checkParam('edit');
        $result = AdminService::edit($params);
        if (true === $result) {
            return $this->success([],'编辑成功');
        } else {
            return $this->error(AdminService::getError());
        }
    }


    /**
     * 删除管理员
     * @return Json
     */
    public function delete()
    {
        $params = (new AdminValidate())->delete()->checkParam('delete');
        $result = AdminService::delete($params);
        if (true === $result) {
            return $this->success([],'删除成功');
        } else {
            return $this->error(AdminService::getError());
        }
    }


    /**
     * 查看管理员详情
     * @return Json
     */
    public function detail()
    {
        $params = (new AdminValidate())->checkParam('detail');
        if($params['id'] == 'admin') $params['id'] = $this->adminId;
        $result = AdminService::detail($params);
        return $this->data($result);
    }


    /**
     * 获取当前管理员信息
     * @return Json
     */
    public function mySelf()
    {
        $result = AdminService::detail(['id' => $this->adminId], 'auth');
        return $this->data($result);
    }

}